//
//  NFindTableHeaderView.m
//  News
//
//  Created by zhangtao on 2018/10/8.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "NFindTableHeaderView.h"

@implementation NFindTableHeaderView
{
    UIButton *selectBtn;
    UIScrollView *scroll;
}
-(instancetype)initWithFrame:(CGRect)frame items:(NSArray *)items{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        
        UIButton *addBtn = [[UIButton alloc]initWithFrame:CGRectMake(frame.size.width-40, frame.size.height/2-15, 30, 30)];
        [addBtn setTitle:@"+" forState:UIControlStateNormal];
        addBtn.titleLabel.font = [UIFont systemFontOfSize:25];
        [addBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:addBtn];
        
        if (items) {
            self.items = items;
        }
        
        UILabel *bottomLine = [[UILabel alloc]initWithFrame:CGRectMake(0, frame.size.height-1, frame.size.width, 1)];
        bottomLine.backgroundColor = RGB(240, 240, 240, 1);
        [self addSubview:bottomLine];
    }
    return self;
}

-(void)setItems:(NSArray *)items{
    if (!scroll) {
        scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 5, self.frame.size.width-50, self.frame.size.height-10)];
        [self addSubview:scroll];
    }else{
        for (UIView *v in scroll.subviews) {
            if ([v isKindOfClass:[UIButton class]]) {
                [v removeFromSuperview];
            }
        }
        scroll.frame = CGRectMake(10, 5, self.frame.size.width-50, self.frame.size.height-10);
    }
    
    
    CGFloat leftWeight = 0;
    for (int i = 0; i<items.count; i++) {
        CGSize size = [ACEPublicTool textViewWeightWithTextFont:MIN_FONT andText:items[i] andSize:CGSizeMake(200, 20)];
        
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(leftWeight, 0, size.width+10, self.frame.size.height-10)];
        [btn setTitle:items[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = MIN_FONT;
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btn setTag:i];
        
        if (i == 0) {
            selectBtn = btn;
            [btn setTitleColor:MAIN_COLOR forState:UIControlStateNormal];
        }
        leftWeight = leftWeight + size.width + 10;
        [scroll addSubview:btn];
    }
    scroll.contentSize = CGSizeMake(leftWeight, 0);
}


-(void)click:(UIButton *)btn{
    [btn setTitleColor:MAIN_COLOR forState:UIControlStateNormal];
    [selectBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    selectBtn = nil;
    selectBtn = btn;
    
    if (self.findHeadfocusCallBack) {
        self.findHeadfocusCallBack(btn.tag);
    }
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
